package leetcode2;

public class MaxScore {
    public int maxScore(int[] cardPoints, int k) {
        int ans = 0;
        int[] dpL = new int[k + 1];
        int[] dpR = new int[k + 1];
        dpL[0] = 0;
        dpR[0] = 0;
        int len = cardPoints.length;
        for (int i = 1; i <= k; i++) {
            dpL[i] = cardPoints[i - 1] + dpL[i - 1];
            dpR[i] = cardPoints[len - i] + dpR[i - 1];
        }
        for (int m = 0; m <= k; m++) {
            ans = Math.max(ans, dpR[m] + dpL[k - m]);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] cardPoints = {9, 7, 7, 9, 7, 7, 9};
        int k = 7;
        MaxScore maxScore = new MaxScore();
        System.out.println(maxScore.maxScore(cardPoints, k));
    }
}
